package com.baking.controller;

import com.baking.model.Collect;
import com.baking.model.Order;
import com.baking.model.User;
import com.baking.service.OrderService;
import com.baking.util.FileUtil;
import com.baking.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author:wrb
 * @Date :2020/1/13/22:59
 * @Describtion:
 **/
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/index")
    public String toOrderPage(){
        return "/order/orderPage";
    }
    @RequestMapping("/orderEditPage")
    public String toOrderEditPage(){
        return "/order/orderEditPage";
    }

    @RequestMapping("/getOrderList")
    @ResponseBody
    public Msg getOrderList (@RequestParam(value = "productId",required = false) Integer productId,
                             @RequestParam(value = "B",required = false) Integer orderId,
                             @RequestParam(value = "phone",required = false) String phone,
                             @RequestParam(value = "productName",required = false) String productName,
                             @RequestParam (value="page",required = false) String page,
                             @RequestParam (value="limit",required = false) String limit){
        Msg msg = new Msg();
        Order order = new Order();
        order.setProductId(productId);
        order.setOrderId(orderId);
        order.setProductName(productName);
        order.setPhone(phone);
        List<Order> orderList = orderService.getOrderList(order);
        msg.setData(orderList);
        msg.setCode("0");
        return msg;
    }

    @RequestMapping("/orderBuyPage")
    public String ordeBuyPage(){
        return "/order/orderBuyPage";
    }

    @RequestMapping("/allOrder")
    public String allOrderPage(){
        System.out.println("allOrder");
        return "/order/allOrder";
    }

    @RequestMapping("/addOrder")
    @ResponseBody
    public Msg addOrder(HttpServletRequest request, HttpServletResponse response,Order order){
        Msg msg = new Msg();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        order.setUserId(user.getUserId());
        order.setUserName(user.getUserName());
        boolean b = orderService.addOrder(order);
        if (b) {
            msg.setMsg("新增成功");
        } else {
            msg.setMsg("新增失败");
        }
        return msg;
    }

    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Msg deleteOrder(Order order){
        Msg msg = new Msg();
        boolean b = orderService.deleteOrder(order);
        if (b) {
            msg.setMsg("删除成功");
        } else {
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @RequestMapping("/updateOrder")
    @ResponseBody
    public Msg updateOrder(HttpServletRequest request, HttpServletResponse response,Order order){
        Msg msg = new Msg();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        order.setUserId(user.getUserId());
        order.setUserName(user.getUserName());
        boolean b = orderService.updateOrder(order);
        if (b) {
            msg.setMsg("更新成功");
        } else {
            msg.setMsg("更新失败");
        }
        return msg;
    }

    @RequestMapping("/addCollect")
    @ResponseBody
    public Msg addCollect(HttpServletRequest request, HttpServletResponse response,Collect collect){
        Msg msg = new Msg();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        collect.setUserId(user.getUserId());
        collect.setUserName(user.getUserName());
        boolean b = orderService.addCollect(collect);
        if (b) {
            msg.setMsg("新增成功");
        } else {
            msg.setMsg("新增失败");
        }
        return msg;
    }

    @RequestMapping("/deleteCollect")
    @ResponseBody
    public Msg deleteCollect(Collect collect){
        Msg msg = new Msg();
        boolean b = orderService.deleteCollect(collect);
        if (b) {
            msg.setMsg("删除成功");
        } else {
            msg.setMsg("删除失败");
        }
        return msg;
    }

}
